/*
 * Copyright (c) 2015 DHC Corporation.
 */

package cn.com.dhc.danlu.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;

public class GoodInfoListView extends ListView {
    /**
     * 可以滚动
     */
    private boolean canScroll;

    /**
     * 手势识别类
     */
    private GestureDetector mGestureDetector;

    /**
     * 触摸事件监听
     */
    private View.OnTouchListener mGestureListener;

    public GoodInfoListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(new YScrollDetector());
        canScroll = true;
        setHorizontalFadingEdgeEnabled(false);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            canScroll = true;
        }
        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);

    }

    private class YScrollDetector extends SimpleOnGestureListener {

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (canScroll) {
                canScroll = Math.abs(distanceY) >= Math.abs(distanceX);
            }

            return canScroll;
        }
    }
}